package com.caigou.comm.base.widget

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.Window
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.caigou.comm.R
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.utils.EditTextUtils

/**
 * @author #Suyghur.
 * Created on 2021/12/07
 */
class BuyFlowerDialog(context: Context) : Dialog(context) {

    private lateinit var etNum: EditText
    private lateinit var tvTotal: TextView
    private lateinit var btnCancel: Button
    lateinit var btnConfirm: Button

    var flowerNum = 0

    init {
        setCanceledOnTouchOutside(true)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        initView()
    }

    private fun initView() {
        setContentView(R.layout.comm_buy_flower_dialog)

        etNum = findViewById(R.id.buy_flower_dialog_et_num)
        etNum.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                val num = s.toString()
                if (TextUtils.isEmpty(num) || !EditTextUtils.isNumber(num)) {
                    tvTotal.text = "合计：0.00元（可用金额${AppBackgroundInfo.instance.userBean!!.balance}）"
                    return
                }
                flowerNum = num.toInt()
                if (flowerNum > 99999) {
                    context.showToast("单次对多可以兑换99999朵")
                    flowerNum = 0
                    etNum.setText("")
                }
                tvTotal.text = "合计：${flowerNum / 10.0}元（可用金额${AppBackgroundInfo.instance.userBean!!.balance}）"
            }
        })
        tvTotal = findViewById(R.id.buy_flower_dialog_tv_total)
        tvTotal.text = "合计：0.00元（可用金额${AppBackgroundInfo.instance.userBean!!.balance}）"
        btnConfirm = findViewById(R.id.buy_flower_dialog_btn_confirm)
        btnCancel = findViewById(R.id.buy_flower_dialog_btn_cancel)
        btnCancel.setOnClickListener {
            this.dismiss()
        }
    }
}